using System;
using System.Collections;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using UnityEditor;
using UnityEditor.PackageManager.UI;
using UnityEngine;

namespace XFGameFramework.EquipmentSystem
{
    public class EquipmentsConfigController 
    {

        private static List<EquipmentsConfig> allConfigs = new List<EquipmentsConfig>();

        public static List<EquipmentsConfig> GetAllEquipmentsConfig() 
        {
            allConfigs.Clear();

            string[] guids = AssetDatabase.FindAssets(string.Format("t:{0}", typeof(EquipmentsConfig).FullName));

            if (guids != null) 
            { 
                foreach (var item in guids)
                {
                    string assetpath = AssetDatabase.GUIDToAssetPath(item);
                    if (string.IsNullOrEmpty(assetpath))
                        continue;
                    EquipmentsConfig config = AssetDatabase.LoadAssetAtPath<EquipmentsConfig>(assetpath);
                    if (config == null) continue;
                    allConfigs.Add(config);
                } 
            }

            return allConfigs;
        }


        public static bool IsContainID(int id) 
        {
            foreach (var item in GetAllEquipmentsConfig())
            {
                if (item.GetEquipmentConfigData(id) != null)
                    return true;
            }

            return false;
        }


        public static int CreateEquipmentConfigData(Type type ,EquipmentsConfig equipmentsConfig)
        {
            if (equipmentsConfig == null) return -1;

            EquipmentsConfig config = equipmentsConfig;
            if (config == null) return -1;
          
            EquipmentConfigData equipment = ScriptableObject.CreateInstance(type) as EquipmentConfigData;

           return CreateEquipmentConfigData(equipment, equipmentsConfig); 
        }


        public static int CreateEquipmentConfigData(EquipmentConfigData equipmentConfigData, EquipmentsConfig equipmentsConfig) 
        {
            int id = GenerateID(equipmentsConfig);
            equipmentConfigData.id = id;
            equipmentConfigData.equipment_name = string.Format("装备:{0}", id);
            equipmentConfigData.parent = equipmentsConfig;

            string asset_path = AssetDatabase.GetAssetPath(equipmentsConfig);
            string guid = AssetDatabase.AssetPathToGUID(asset_path);

            string path = string.Format("Assets/Extensions/EquipmentSystem/{0}", guid);
            CreateAsset(equipmentConfigData, path, false);

            if (!equipmentsConfig.Equipments.Contains(equipmentConfigData))
                equipmentsConfig.Equipments.Add(equipmentConfigData);
            EditorUtility.SetDirty(equipmentsConfig);
            equipmentsConfig.ClearCache();
            equipmentConfigData.Rename();
            EditorUtility.SetDirty(equipmentConfigData);
            AssetDatabase.SaveAssets();
            AssetDatabase.Refresh();

            return id;
        }


        private static int GenerateID(EquipmentsConfig config)
        {
            int max_id = config.start_id;

            foreach (var item in config.Equipments)
            {
                if (item.id > max_id)
                    max_id = item.id;
            }

            max_id++;

            return max_id;
        }


        public static void CreateAsset(ScriptableObject scriptableObject, string path, bool ping = true)
        {
            if (!System.IO.Directory.Exists(path))
            {
                System.IO.Directory.CreateDirectory(path);
                AssetDatabase.Refresh();
            }

            string name = AssetNameTools.GetName(scriptableObject.GetType().Name, path);
            AssetDatabase.CreateAsset(scriptableObject, string.Format("{0}/{1}.asset", path, name));
            Selection.activeObject = scriptableObject;
            if (ping)
            { 
                EditorGUIUtility.PingObject(scriptableObject); 
            }
        }

         
        public static string GetCopyName(string equipment_name, EquipmentsConfig equipmentsConfig)
        {

            int count = 1;

            if (Regex.IsMatch(equipment_name, "\\([0-9]+\\)$"))
            {
                for (int i = equipment_name.Length - 1; i >= 0; i--)
                {
                    if (equipment_name[i] == '(')
                    {
                        string c = equipment_name.Substring(i + 1, equipment_name.Length - i - 2);
                        count = int.Parse(c);

                        equipment_name = equipment_name.Substring(0, i);
                        break;
                    }
                }
            }

            equipment_name = equipment_name.TrimEnd();

            string name = string.Empty;

            do
            {
                name = string.Format("{0} ({1})", equipment_name, count);
                count++;
            } while (IsContainsBuffName(name, equipmentsConfig));

            return name;
        }

        private static bool IsContainsBuffName(string equipment_name, EquipmentsConfig equipmentsConfig)
        {

            foreach (var item in equipmentsConfig.Equipments)
            {
                if (item.equipment_name == equipment_name)
                    return true;
            }

            return false;
        }


    }

}

